package org.zn.mvcbase.validate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.zn.mvcbase.response.ResponseMessage;
import org.zn.mvcbase.response.ValidatedMessage;
import org.zn.mvcbase.response.status.Validation;

public class ValidatedResult {

	
	private Boolean hasErrors = false;

	private Map<String, String[]> errorsMap;
	
	private BindingResult br;
	
	private String forwardurl;
	
	private ValidatedMessage vm;
		
	public Boolean hasErrors() {
		return hasErrors;
	}

	public Map<String, String[]> getErrorsMap() {
		return errorsMap;
	}
	
	public BindingResult getBr() {
		return br;
	}

	public String getForwardurl() {
		return forwardurl;
	}

	public void setForwardurl(String forwardurl) {
		this.forwardurl = forwardurl;
	}

	public Map<String, String[]> dealBindingResult(BindingResult br) {

		this.br=br;
		
		errorsMap = new HashMap<String, String[]>();

		if (br.hasErrors()) {
			hasErrors=true;
			
			
			List<FieldError> errorlist =  br.getFieldErrors();

			for (FieldError fieldError : errorlist) {
				
			
				errorsMap.put(fieldError.getField(),
						new String[] { fieldError.getDefaultMessage(),
					fieldError.getCode()});
	 
			}

		}
		
		 vm=	new ValidatedMessage();
		 vm.setStatus(Validation.MAINFAILD);
	     vm.setValidmessage(errorsMap);
		 
		return errorsMap;

	}


	public ResponseMessage getValidatedMessage(){
	
		return this.vm;
	
	}
	
	

}
